﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;   
using System.Web.UI;
using System.Web.UI.WebControls;
using CategoryServiceReference;
using ProductServiceReference;
using System.ServiceModel;

public partial class UserControl_AddProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.Enctype = "multipart/form-data";
        if (IsPostBack) return;
        var client = new CategoryServiceClient();
        foreach (var item in client.GetCategories())
        {
            var lstItem = new ListItem()
                              {
                                  Text = item.CategoryName,
                                  Value = item.CategoryID + ""
                              };
            ddlcat.Items.Add(lstItem);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
            try
        {
            var client = new ProductServiceClient();
            var result = client.AddProduct(
                new Product()
                    {
                        ProductName = txtName.Text.Trim(),
                        ProductImage = "Uploads/product/" + upload.FileName,
                                //: "images/default.jpg",
                        CategoryID = int.Parse(ddlcat.SelectedValue),
                        Origin = txtOrigin.Text.Trim(),
                        Price = decimal.Parse(txtPrice.Text.Trim()),
                        Quantity = int.Parse(txtQuantity.Text.Trim()),
                        GuaranteePeriod = short.Parse(txtGuaranteePeriod.Text.Trim())
                    }
                );
            upload.SaveAs(MapPath("~/Uploads/product/")+ upload.FileName);
            msg.Text = result ? "Add product successfully!" : "Add product not success!";
        }
        catch (FaultException<Product> faultException)
        {
            msg.Text = faultException.Message;
        }
        catch (Exception exception)
        {
            msg.Text = exception.Message;
        }
    }
}